package day1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.TimeUnit;

@Controller
public class Redis1Controller {

    private static String preFix = "sendSMS:";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping("sendSMS")
    public void sendSMS(String phone){
        //生成一个随机的数字。
        String number = "123456";
        //存储到redis中。并设置有效期，300s
        stringRedisTemplate.opsForValue().set(preFix+phone,number,
                300, TimeUnit.SECONDS);
        //调用第三方的插件，发送短信。
        //TODO：
    }

    @PostMapping("verifySMS")
    public void verifySMS(String phone,String verifyCode){
        String number = stringRedisTemplate.opsForValue().get(preFix + phone);
        if(verifyCode.equals(number)){
            System.out.println("验证成功！");
            return;
        }
        System.out.println("验证失败！");
    }

}
